﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
using System.Collections.Generic;

namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cms_purview_setting。
    /// </summary>
    public class cms_purview_setting
    {
        public cms_purview_setting()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int p_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cms_purview_setting");
            strSql.Append(" where p_id=" + p_id + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cms_purview_setting model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.p_pid != null)
            {
                strSql1.Append("p_pid,");
                strSql2.Append("" + model.p_pid + ",");
            }
            if (model.p_table != null)
            {
                strSql1.Append("p_table,");
                strSql2.Append("'" + model.p_table + "',");
            }
            if (model.p_value != null)
            {
                strSql1.Append("p_value,");
                strSql2.Append("'" + model.p_value + "',");
            }
            if (model.p_type != null)
            {
                strSql1.Append("p_type,");
                strSql2.Append("" + model.p_type + ",");
            }
            strSql.Append("insert into cms_purview_setting(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cms_purview_setting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cms_purview_setting set ");
            if (model.p_pid != null)
            {
                strSql.Append("p_pid=" + model.p_pid + ",");
            }
            if (model.p_table != null)
            {
                strSql.Append("p_table='" + model.p_table + "',");
            }
            if (model.p_value != null)
            {
                strSql.Append("p_value='" + model.p_value + "',");
            }
            if (model.p_type != null)
            {
                strSql.Append("p_type=" + model.p_type + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where p_id=" + model.p_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int p_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cms_purview_setting ");
            strSql.Append(" where p_id=" + p_id + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cms_purview_setting GetModel(int p_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" p_id,p_pid,p_table,p_value,p_type ");
            strSql.Append(" from cms_purview_setting ");
            strSql.Append(" where p_id=" + p_id + " ");
            Usas.Model.Cms.cms_purview_setting model = new Usas.Model.Cms.cms_purview_setting();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["p_id"].ToString() != "")
                {
                    model.p_id = int.Parse(ds.Tables[0].Rows[0]["p_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["p_pid"].ToString() != "")
                {
                    model.p_pid = int.Parse(ds.Tables[0].Rows[0]["p_pid"].ToString());
                }
                model.p_table = ds.Tables[0].Rows[0]["p_table"].ToString();
                model.p_value = ds.Tables[0].Rows[0]["p_value"].ToString();
                if (ds.Tables[0].Rows[0]["p_type"].ToString() != "")
                {
                    model.p_type = int.Parse(ds.Tables[0].Rows[0]["p_type"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select p_id,p_pid,p_table,p_value,p_type ");
            strSql.Append(" FROM cms_purview_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" p_id,p_pid,p_table,p_value,p_type ");
            strSql.Append(" FROM cms_purview_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */
        public int DelAddP_Value(List<string> gl, int id, string table, int type)
        {
            List<string> l = new List<string>();
            l.Add("delete from cms_purview_setting where p_pid=" + id + " and p_table='" + table + "' and p_type=" + type);

            for (int i = 0; i < gl.Count; i++)
            {
                l.Add("insert into cms_purview_setting values(" + id + ",'" + table + "','" + gl[i] + "'," + type + ")");
            }
            return DbHelperSQL.ExecuteSqlTran(l);
        }

        #endregion  成员方法
    }
}

